package algotithm.leetcode.dp.test115;

/**
 * @author zhouyanxiang
 * @Date 2021-03-2021/3/17-11:16
 * @URL https://leetcode-cn.com/problems/distinct-subsequences/
 * @Title 115. 不同的子序列
 */
public class Solution {

    public static void main(String[] args) {
        String s = "rabbbit";
        String t = "rabbit";
        Solution solution = new Solution();
        int i = solution.numDistinct(s, t);
        System.out.println(i);
    }

    public int numDistinct(String s, String t) {
        int m = s.length();
        int n = t.length();
        if (m < n) {
            return 0;
        }
        int[][] dp = new int[m+1][n+1];
        for (int i = 0; i <= m; i++) {
            dp[i][n] = 1;
        }
        for (int i = m - 1; i >= 0; i--) {
            char sChar = s.charAt(i);
            for (int j = n - 1; j >= 0; j--) {
                char tChar = t.charAt(j);
                if (sChar == tChar) {
                    dp[i][j] = dp[i+1][j+1] + dp[i+1][j];
                } else {
                    dp[i][j] = dp[i+1][j];
                }
            }
        }
        return dp[0][0];
    }

}
